/** 
 * @CopyRight (c) 2012-2012
 * @Project <azj_commons>
 * @JDK_Version_Used:<JDK1.6>
 * @Description:<web页面资源文件脚本代理>
 * @Title: WebPropertiesToosl.java
 * @Author: <何俊>
 * @Author_of_Contact <E:azj1001@sina.com T:18651831939>
 * @Date 2012-11-2 下午9:17:12
 * @Modified_By: <修改人>
 * @Modified_of_Contact <修改人联系方式>
 * @Modified_Date <修改日期时间>
 * @Why & What is modified <修改原因描述>
 * @Version v1.00
 */
package com.e.buy.tools;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.springframework.core.io.Resource;
import cn.commons.dbclient.exception.AzjDbClientException;

/**
 * @Description:(web页面资源文件脚本代理)
 * @author <何俊>
 * @date 2012-11-2 下午9:17:12
 * @version v1.0
 * @Other :@see #Files
 */
public final class WebPropertiesTools implements Serializable {
	/**
	 * @fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
	 */
	private static final long serialVersionUID = 1L;

	/** 日志记录 */
	private static final Logger log4j = Logger.getLogger(WebPropertiesTools.class);

	/** 读取properties配置properties文件地址 */
	private List<Map<String, Resource>> locations;

	/** properties 集合管理 */
	private Map<String, Properties> properties = new HashMap<String, Properties>();
	
	// 线程安全锁
	private static final Object lockObj = new Object();

	private static WebPropertiesTools instance = null;

	public static WebPropertiesTools getInstance() throws AzjDbClientException {
		if (null == instance) {
			synchronized (lockObj) {
				if (null == instance) {
					instance = new WebPropertiesTools();
					log4j.info("load WebPropertiesTools complete!");
				}
			}
		}
		return instance;
	}
	
	/**
	 * 初始化加载配置文件
	 * 
	 * @title: initMethod
	 * @throws IOException
	 * @return void 返回类型
	 * @throws AzjDbClientException
	 */
	private void initMethod() throws AzjDbClientException {
		loadPorperties();
	}

	/**
	 * 刷新
	 * 
	 * @title: refresh
	 * @return void 返回类型
	 */
	public void refresh() {
		properties.clear();
		try {
			initMethod();
		} catch (AzjDbClientException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 加载 properties 文件
	 * 
	 * @title: loadPorperties
	 * @return void 返回类型
	 * @throws AzjDbClientException
	 */
	private void loadPorperties() throws AzjDbClientException {
		try {
			if (null != locations && !locations.isEmpty()) {
				log4j.debug("load Web properties configuration ...");

				for (Map<String, Resource> map : locations) {
					for (Iterator<String> keys = map.keySet().iterator(); keys.hasNext();) {
						String key = keys.next();
						Properties p = new Properties();
						p.load(map.get(key).getInputStream());
						properties.put(key, p);
					}
				}
			}
		} catch (IOException e) {
			log4j.error("load Web properties error!! msg=[" + e + "]");
			throw new AzjDbClientException("load Web properties error!! msg=[" + e + "]");
		}
	}

	/**
	 * @throws AzjDbClientException
	 *             获取指定properties的指定名称内容
	 * @title: getValue
	 * @param mKey
	 *            : properties Map 文件指定的key
	 * @param pkey
	 *            : properties 内容指定的 key
	 * @return String 返回类型
	 * @throws
	 */
	public String getPropertiesVal(String mKey, String pKey) throws AzjDbClientException {
		if (mKey != null && pKey != null) {
			String def = "The key [" + pKey + "] is not exists in [" + mKey + "] list";
			return properties.get(mKey.trim()).getProperty(pKey.trim(), def);
		} else {
			throw new AzjDbClientException("Sorry!The Web Key Checking Is NullPointException!");
		}
	}

	public void setLocations(List<Map<String, Resource>> locations) {
		this.locations = locations;
	}

}
